<?php
class GraphController extends AppController {

	var $name = 'Graph';
	var $uses = array('Workout');
	
	function index() {
		$this->redirect(array('action'=>'month'));
	}
	
	function month($year=null, $month=null) {
		if(!is_numeric($year)) {
			$year = $header = date('Y');
		}
		
		if(!is_numeric($month)) {
			$month = date('m');
		}
		if($month < 1) $this->redirect(array('action'=>'month',$year-1, 12));
		if($month > 12) $this->redirect(array('action'=>'month', $year+1, 1));
		
		$date_range = $this->DateUtility->dateRangeForMonth($year, $month, false);
		$workouts = $this->Workout->findBetweenDates($date_range[0]['date'], $date_range[1]['date']);
		//$stats = $this->stats($workouts);
		$this->pageTitle = __('Graph of workouts for', true) . ' ' . $this->user['User']['username'] . ' ' . __('in', true) . ' ' . date('F Y', mktime(0, 0, 0, $month, 1, $year));
		$header = date('F, Y', mktime(0, 0, 0, $month, 1, $year));
		//debug($workouts);
		$this->set(compact('workouts', 'year', 'month', 'header'));
	}
	
	function week($year=null, $week=null) {
		if(!is_numeric($year)) {
			$year = $header = date('Y');
		}
		
		if(!is_numeric($week)) {
			$week = date('W');
		}
		$max_week_num = $this->DateUtility->numberOfWeeksInYear($year);
		
		if($week < 1) $this->redirect(array('action'=>'week',$year-1, $max_week_num));
		if($week > $max_week_num) $this->redirect(array('action'=>'week', $year+1, 1));

		$date_range = $this->DateUtility->dateRangeForWeek($year, $week);
		$workouts = $this->Workout->findBetweenDates($date_range[0]['date'], $date_range[1]['date']);

		$header = __('week', true).S.$week.S.__('of', true).S.$year;
		$this->pageTitle = __('Graph of workouts for', true) . S . $this->user['User']['username'] . S . __('in', true) . S . $header;
		
		//debug($workouts);
		$this->set(compact('workouts', 'year', 'week', 'header'));
	}
	
}
?>